package com.luoye.restful;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ClassFinder {
	public List<String> findAllClass(String packageName){
		List<String> classNameList=new ArrayList<String>();
		String packageDir=packageName.replaceAll("\\.","/");
		String rootDir=getClass().getResource("/").getPath()+packageDir;
		findAllClass(classNameList,rootDir,"",packageName);
		return classNameList;
	}
	
	private void findAllClass(List<String> classNameList,String dirPath,String packageName,String basePackageName){
		System.out.println("dirPath->"+dirPath+"\n packageName->"+packageName);
		File dir=new File(dirPath);
		File[] fileList=dir.listFiles();
		if(fileList!=null){
			for(File file:fileList){
				if(file.isDirectory()){
					String dirName=file.getName();
					String dirPath1=dirName;
					String packageName1=dirName;
					if(!"".equals(dirPath)){
						dirPath1=dirPath+"/"+dirName;
					}
					if(!"".equals(packageName)){
						packageName1=packageName+"."+dirName;
					}
					findAllClass(classNameList,dirPath1,packageName1,basePackageName);
					
				}else{
					String className=file.getName();
					int idx=className.lastIndexOf(".");
					if(idx>0){
						className=className.substring(0,idx);
						if(!"".equals(packageName)){
							className=packageName+"."+className;
						}
						if(!"".equals(basePackageName)){
							className=basePackageName+"."+className;
						}
						classNameList.add(className);
					}
				}
			}
		}
	}
}
